/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.*;
import FrameWork.Audio.RSAudio;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author DANTRUONG
 */
public class State_Help extends IState{
    Sprite m_Ball;
    Sprite[] m_Tut = new Sprite[4];
    Sprite m_Home;
    Sprite m_Point;
    
    int m_IDChoosed;
    int m_CurTut, m_NextTut;
    Rectangle[] m_RectButton = new Rectangle[3];
    Point[] m_CurPos = new Point[4];
    Rectangle m_Screen;
    Rectangle[] m_RectTut = new Rectangle[4];
    int m_Status;
    
    public State_Help(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_HELP;
    }
 
    public void Init (){
        super.Init();       
        try {
            m_Tut[0] = new Sprite(Image.createImage("/Image/tut0.png"),
                    240, 320);
            m_Tut[1] =  new Sprite(Image.createImage("/Image/tut1.png"),
                    240, 320);
            m_Tut[2] = new Sprite(Image.createImage("/Image/tut2.png"),
                    240, 320);
            m_Tut[3] = new Sprite(Image.createImage("/Image/tut3.png"),
                    240, 320);
            m_Home = new Sprite(Image.createImage("/Image/home.png"),
                    42, 42);
            m_Point = new Sprite(Image.createImage("/Image/next.png"),
                    36,48);
            m_Ball = new Sprite (Image.createImage("/Image/ball.png"),
                    60, 60);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        m_IDChoosed = -1;
        for(int i=0; i<3; i++)
            m_RectButton[i] = new Rectangle(20 + 70*i, 260, 60, 60);
        m_Screen = new Rectangle(0, 0, 239, 320);
        for(int i=0; i<4; i++){
            m_CurPos[i] = new Point(0,0);
            m_RectTut[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 240, 320);
        }
        m_CurTut = 0;
        m_NextTut = 0;
        m_Status = 0;
    }
    
    public void PointerPressed(int x, int y){
        if(m_Status == 0){
            for(int i=0; i<3; i++){
                if(m_RectButton[i].Intersects(new Point(x,y))){
                    m_IDChoosed = i;
                    if(m_IDChoosed == 1){
                        RSAudio.getRS().PlayExplosion();
                        m_Play.setNextState(new State_Menu(m_Play));
                    }
                    if(m_IDChoosed == 0){
                        if(m_CurTut != 0){
                            RSAudio.getRS().PlayBubble();
                            m_NextTut = m_CurTut - 1;
                            m_CurPos[m_NextTut] = new Point(-240, 0);
                            m_Status = 1;
                        }
                    }
                    if(m_IDChoosed == 2){
                        if(m_CurTut != 3){
                            RSAudio.getRS().PlayBubble();
                            m_NextTut = m_CurTut + 1;
                            m_CurPos[m_NextTut] = new Point(240, 0);
                            m_Status = 2;
                        }
                    }
                }
            }
        }
    }
    
    public void KeyPressed (int _Keycode){
        if(m_Status == 0){
            if (_Keycode == -5 || _Keycode == 53){
                RSAudio.getRS().PlayExplosion();
                m_IDChoosed = 1;
                m_Play.setNextState(new State_Menu(m_Play));
            }
            if (_Keycode == -3 || _Keycode == 52){
                if(m_CurTut != 0){
                    m_IDChoosed = 0;
                    RSAudio.getRS().PlayBubble();
                    m_NextTut = m_CurTut - 1;
                    m_CurPos[m_NextTut] = new Point(-240, 0);
                    m_Status = 1;
                }                  
            }
            if (_Keycode == -4 || _Keycode == 54){
                if(m_CurTut != 3){
                    m_IDChoosed = 2;
                    RSAudio.getRS().PlayBubble();       
                    m_NextTut = m_CurTut + 1;
                    m_CurPos[m_NextTut] = new Point(240, 0);
                    m_Status = 2;
                }
            }
        }
    }
    
    public void Update (float _Time){
        switch(m_Status){
            case 1:
                if(m_CurPos[m_NextTut].X < 0){
                        m_CurPos[m_NextTut] = new Point(m_CurPos[m_NextTut].X + 12,m_CurPos[m_NextTut].Y);
                        m_RectTut[m_NextTut] = new Rectangle(m_CurPos[m_NextTut].X, m_CurPos[m_NextTut].Y, 240, 320);
                        m_CurPos[m_CurTut] = new Point(m_CurPos[m_CurTut].X + 12,m_CurPos[m_CurTut].Y);
                        m_RectTut[m_CurTut] = new Rectangle(m_CurPos[m_CurTut].X, m_CurPos[m_CurTut].Y, 240, 320);
                }
                if(m_CurPos[m_NextTut].X > 0){
                        m_CurPos[m_NextTut] = new Point(0, m_CurPos[m_NextTut].Y);
                        m_RectTut[m_NextTut] = new Rectangle(m_CurPos[m_NextTut].X, m_CurPos[m_NextTut].Y, 240, 320);
                        m_CurPos[m_CurTut] = new Point(m_CurPos[m_CurTut].X - m_CurPos[m_NextTut].X,m_CurPos[m_CurTut].Y);
                        m_RectTut[m_CurTut] = new Rectangle(m_CurPos[m_CurTut].X, m_CurPos[m_CurTut].Y, 240, 320);
                }
                if(m_CurPos[m_NextTut].X == 0){
                    m_CurTut = m_NextTut;
                    m_IDChoosed = -1;
                    m_Status = 0;
                }
                break;
            case 2:
                if(m_CurPos[m_NextTut].X > 0){
                        m_CurPos[m_NextTut] = new Point(m_CurPos[m_NextTut].X - 12,m_CurPos[m_NextTut].Y);
                        m_RectTut[m_NextTut] = new Rectangle(m_CurPos[m_NextTut].X, m_CurPos[m_NextTut].Y, 240, 320);
                        m_CurPos[m_CurTut] = new Point(m_CurPos[m_CurTut].X - 12,m_CurPos[m_CurTut].Y);
                        m_RectTut[m_CurTut] = new Rectangle(m_CurPos[m_CurTut].X, m_CurPos[m_CurTut].Y, 240, 320);
                }
                if(m_CurPos[m_NextTut].X < 0){
                        m_CurPos[m_NextTut] = new Point(0, m_CurPos[m_NextTut].Y);
                        m_RectTut[m_NextTut] = new Rectangle(m_CurPos[m_NextTut].X, m_CurPos[m_NextTut].Y, 240, 320);
                        m_CurPos[m_CurTut] = new Point(m_CurPos[m_CurTut].X + m_CurPos[m_NextTut].X,m_CurPos[m_CurTut].Y);
                        m_RectTut[m_CurTut] = new Rectangle(m_CurPos[m_CurTut].X, m_CurPos[m_CurTut].Y, 240, 320);
                }
                if(m_CurPos[m_NextTut].X == 0){
                    m_CurTut = m_NextTut;
                    m_IDChoosed = -1;
                    m_Status = 0;
                }
                break;
        }
    }
    
    public void Render (){
       super.Render();
       m_Graphics.setColor(0, 0, 0);
       m_Graphics.fillRect(0, 0, 240, 320);

       if(m_RectTut[m_CurTut].Intersects(m_Screen))
       {
            m_Tut[m_CurTut].setFrame(0);
            m_Tut[m_CurTut].setPosition(m_CurPos[m_CurTut].X, m_CurPos[m_CurTut].Y);
            m_Tut[m_CurTut].paint(m_Graphics);
       }
       if(m_RectTut[m_NextTut].Intersects(m_Screen))
       {
            m_Tut[m_NextTut].setFrame(0);
            m_Tut[m_NextTut].setPosition(m_CurPos[m_NextTut].X, m_CurPos[m_NextTut].Y);
            m_Tut[m_NextTut].paint(m_Graphics);
       }
       if(m_CurTut != 0){
           m_Point.setFrame(0);
           m_Point.setPosition(30, 267);
           m_Point.paint(m_Graphics);
       }
       
       if(m_CurTut != 3){ 
           m_Point.setFrame(0);
           m_Point.setPosition(210, 267);
           m_Point.setTransform(Sprite.TRANS_MIRROR);
           m_Point.paint(m_Graphics);
           m_Point.setTransform(Sprite.TRANS_NONE);
       }
              
       m_Home.setFrame(0);
       m_Home.setPosition(99, 269);
       m_Home.paint(m_Graphics);
       
       for(int i=0; i<3 ; i++){
           if((m_CurTut == 0 && i== 0) || (m_CurTut == 3 && i == 2)){
           }else{
               if(m_IDChoosed==i){
                    m_Ball.setFrame(1);
                    m_Ball.setPosition(20 + 70*i, 260);
                    m_Ball.paint(m_Graphics);
               }else {
                    m_Ball.setFrame(0);
                    m_Ball.setPosition(20 + 70*i, 260);
                    m_Ball.paint(m_Graphics);
               }
           }
       }   
    }
    public void Destroy (){        
        m_Ball=null;
        m_Tut = null;
        m_Home=null;
        m_Point=null;
    }
}
